<template>
	<div :class="curClassName" :style="curStyle">
		<van-switch
			:class="curClassName"
			:style="curStyle"
			v-model="curValue"
			:disabled="disabled"
		>
		</van-switch>
	</div>
</template>
<script setup lang="ts">
import { PresetSwitchProps } from './preset-switch';
import { IEvent } from '@/core';
const props = defineProps(PresetSwitchProps);

interface PresetSwitchEmit {
	(name: 'component-action', value: IEvent<string>): void;
}
const emit = defineEmits<PresetSwitchEmit>();

/**
 * 项名称
 *
 * @memberof AppPresetSwitch
 */
const itemName = computed(() => {
	return props.index || props.index === 0
		? `${props.name}_${props.index}`
		: props.name;
});

/**
 * 当前容器样式
 *
 * @memberof AppPresetSwitch
 */
const curStyle = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	if (layoutModel) {
		return layoutModel.getElementStyle();
	}
});

/**
 * 类名
 *
 * @memberof AppPresetSwitch
 */
const curClassName = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	const classNames = {
		'app-preset-switch': true
	};
	if (layoutModel) {
		Object.assign(classNames, layoutModel.getClassNames());
	}
	return classNames;
});

/**
 * 禁用
 *
 * @memberof AppPresetSwitch
 */
const disabled = computed(() => {
	const layoutModel = props.layoutModelDetails[itemName.value];
	if (layoutModel) {
		return layoutModel.disabled;
	}
});

/**
 * 当前值
 *
 * @type {any}
 * @memberof AppPresetSwitch
 */
const curValue = computed({
	get: () => (props.value ? true : false),
	set: (value: any) => {
		emit('component-action', {
			name: `${props.name}`,
			action: 'valueChange',
			data: value,
		});
	},
});
</script>
